package com.rongcheng.forfeature;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;

import com.rongcheng.forfeature.actions.ActionCreator;
import com.rongcheng.forfeature.dispatcher.Dispatcher;
import com.rongcheng.forfeature.stores.Store;


/**
 * Created by frc on 16-10-31.
 */

public abstract class BaseActivity extends Activity {
    protected Store store;
    protected Dispatcher dispatcher = Dispatcher.get();
    protected ActionCreator actionCreator = ActionCreator.get(dispatcher);
    protected static String TAG = "no activity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = this.getClass().getSimpleName();
        Log.e(TAG, "onCreate");
        doBeforeSetContentView();
        setContentView(setContentViewID());
        getStore();
        initView();
        initData();
        setViewListener();
    }


    protected abstract void doBeforeSetContentView();

    protected abstract int setContentViewID();

    protected abstract Store getStore();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void setViewListener();

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");
        if (store != null) {
            try {
                store.register(this);
                dispatcher.register(store);
            } catch (Exception e) {
                Log.e(TAG, "can not register again");
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause");
        if (store != null) {
            try {
                store.unRegister(this);
                dispatcher.unRegister(store);
            } catch (Exception e) {
                Log.e(TAG, "can not unRegister again");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
    }
}
